<?php  
/**
 * 处理Post PI算法 封装类
 * PI算法:Rank Index = [(A / 10) + 0.5] × (R/5) × (P / O) 
 *        1) 根据搜索结果根据游戏自身网站的权威值给值A（1-10）;
 *        2) 根据游戏在本站的打开次数给值P;
 *        3) 根据游戏加入本站时间从新到老给值O;
 *        4）根据用户打分给值R (1-5)；
 */
class PIHandClass{ 
	
   const PAGE_POSTS_NUM = 1000;
  
	function updateAllPostsPI(){  
 	    $pageTotal = $this->getPageNum();	
// 	    echo "一共查询到".$pageTotal."页数据<br/>";   
 		for($currentPage = 0; $currentPage < $pageTotal; $currentPage++){ 
// 			  echo "第".$currentPage."次循环<br/>";    
			$everyPagePosts = $this->getPostsByPageNum($currentPage);
			$this->handPostsPI($everyPagePosts);
		} 
	}
	function getPageNum(){
		  $pageNum =  intval($this->getPostsTotal()/(PIHandClass::PAGE_POSTS_NUM)); 
          if(!(100%3)){
			$pageNum += 1;
          }
		 return $pageNum;
	}
	function getPostsTotal(){
		global $wpdb; 
		$countPostsSql = "select count(*) as id from wp_posts where post_status ='publish' and post_type='post'";
		$countPostsNums = $wpdb->get_results($countPostsSql);
		$countnum;
		foreach($countPostsNums as $num){
			$countnum = $num->id;
		} 
		return $countnum;
	}
	function getPostsByPageNum($paged){
			global $wpdb;		 
			$frompageNum = ($paged)*(PIHandClass::PAGE_POSTS_NUM);
			$topageNum = ($paged+1)*(PIHandClass::PAGE_POSTS_NUM); 
			$getPostsByPageSql = "select p.id from wp_posts p  where post_status ='publish' and post_type='post' limit $frompageNum,$topageNum"; 
			  echo "sql$getPostsByPageSql<br/>";
			$everyPagePosts = $wpdb->get_results($getPostsByPageSql);
			return $everyPagePosts; 
	}
	function handPostsPI($posts){ 
	     foreach($posts as $post){  
		 	$this->handPostPIByPostId($post->id);
		} 
	}
	//this method have problem..modify 2011.10.30 yuyue
	function handPostPIByPostId($postId){
		  global $wpdb; 
		 $postPI = floatval($this->getPostPI($postId)); 
		  
		 //TODO 如果PI不存在就insert,如果PI跟原来的不一样，就update，否则，不用处理
//		 if(empty($postPI)){
//		   add_post_meta($postId, "GamePI", $postPI); 
//		}else{
//		   update_post_meta($postId, "GamePI", $postPI); 
//		}  
        $wpdb->update( 
			'wp_posts', 
			array( 
				'post_pi' => $postPI  
			), 
			array( 'ID' => $postId ) 
		);
		 
	}
   
		
		
 
	//update the method because of the $postR must / 10评分入库时多都乘了10分
	function getPostPI($postId){
				$postA = $this->getPostA($postId);
				$postR = $this->getPostR($postId);
				$postP = $this->getPostP($postId);
				$postO = $this->getPostO($postId); 
				$postPI ; 
				$postPI = (($postA / 10) + 0.5) * ($postR/5/10) * ($postP / $postO);
				return $postPI;
	}
	 
	function getPostA($postId){
		$postA = get_post_meta($postId, "GamePR", true);
		if(empty($postA)){
			$postA = 9;
		}
		return $postA;			
	}
	function getPostR($postId){
		$postR;
		$postTotalScore = get_post_meta($postId, "GameTotalScore", true);
		$postTotalScoreTimes = get_post_meta($postId, "GameTotalScoreTimes", true);
		if(!empty($postTotalScoreTimes)){
			$postR = $postTotalScore/$postTotalScoreTimes;
		}else{
			$postR = 1;					
		}
		return $postR;
		
	}
	function getPostO($postId){
	    $postO = get_post_meta($postId, "GameDate", true); 
		$postCreateDate = get_post_meta($postId, "GameDate", true);
		$curr_date = new DateTime(); 
		$game_create_date;

		try{
			  $game_create_date = new DateTime($postO);
		  }catch(Exception $e){
			  	 $game_create_date = new DateTime(); 
		  } 
		   //当前天数		
		$startdate=strtotime($game_create_date->format("Y-m-d"));
		$enddate=strtotime($curr_date->format("Y-m-d"));    //上面的php时间日期函数已经把日期变成了时间戳，就是变成了秒。这样只要让两数值相减，然后把秒变成天就可以了，比较的简单，如下：
		$days=round(($enddate-$startdate)/3600/24) ; 
	    $days == 0?$postO = 1:$postO = $days;
		return $postO;
			
	}
	function getPostP($postId){
		$postP = get_post_meta($postId, "post_views_count", true);
		if(empty($postP) || $postP < 0){
		 		$postP = 1;
		}	  
		return $postP;			
		
	}
}